﻿
public class Student
{
    public int StudentId { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public string Class { get; set; }
    public override string ToString()
    {
        return $"学号：{StudentId} | 姓名：{Name} | 年龄：{Age} | 班级：{Class}";
    }
}

public class StudentManager
{
    private readonly List<Student> _students = new List<Student>();
    public bool AddStudent(Student student)
    {
        if (student == null || _students.Exists(s => s.StudentId == student.StudentId))
        {
            return false;
        }
        _students.Add(student);
        return true;
    }

    public bool UpdateStudent(int studentId, string newName, int newAge, string newClass)
    {
        var student = _students.FirstOrDefault(s => s.StudentId == studentId);
        if (student == null) return false;

        student.Name = newName;
        student.Age = newAge;
        student.Class = newClass;
        return true;
    }
    public bool DeleteStudent(int studentId)
    {
        var student = _students.FirstOrDefault(s => s.StudentId == studentId);
        if (student == null) return false;

        _students.Remove(student);
        return true;
    }

    public Student QueryStudent(int studentId)
    {
        return _students.FirstOrDefault(s => s.StudentId == studentId);
    }

    public List<Student> GetAllStudents()
    {
        return new List<Student>(_students);
    }
}

class Program
{
    static void Main(string[] args)
    {
        var studentManager = new StudentManager();
        Console.WriteLine("===== 学生管理系统测试 =====");
        Console.WriteLine("\n1. 测试增加学生：");
        bool add1 = studentManager.AddStudent(new Student { StudentId = 101, Name = "张三", Age = 18, Class = "高一(1)班" });
        bool add2 = studentManager.AddStudent(new Student { StudentId = 102, Name = "李四", Age = 17, Class = "高一(2)班" });
        bool add3 = studentManager.AddStudent(new Student { StudentId = 101, Name = "王五", Age = 18, Class = "高一(1)班" });
        Console.WriteLine($"添加101号学生：{(add1 ? "成功" : "失败")}");
        Console.WriteLine($"添加102号学生：{(add2 ? "成功" : "失败")}");
        Console.WriteLine($"添加重复学号101：{(add3 ? "成功" : "失败")}");
        Console.WriteLine("\n2. 测试遍历所有学生：");
        foreach (var student in studentManager.GetAllStudents())
        {
            Console.WriteLine(student);
        }
        Console.WriteLine("\n3. 测试查询学生：");
        var query1 = studentManager.QueryStudent(101);
        var query2 = studentManager.QueryStudent(999);
        Console.WriteLine($"查询101号学生：{(query1 != null ? query1.ToString() : "未找到")}");
        Console.WriteLine($"查询999号学生：{(query2 != null ? query2.ToString() : "未找到")}");
        Console.WriteLine("\n4. 测试修改学生：");
        bool update1 = studentManager.UpdateStudent(101, "张三", 19, "高二(1)班");
        bool update2 = studentManager.UpdateStudent(999, "赵六", 18, "高一(3)班");
        Console.WriteLine($"修改101号学生：{(update1 ? "成功" : "失败")}");
        Console.WriteLine($"修改999号学生：{(update2 ? "成功" : "失败")}");
        Console.WriteLine("修改后101号学生信息：" + studentManager.QueryStudent(101));
        Console.WriteLine("\n5. 测试删除学生：");
        bool delete1 = studentManager.DeleteStudent(102);
        bool delete2 = studentManager.DeleteStudent(999);
        Console.WriteLine($"删除102号学生：{(delete1 ? "成功" : "失败")}");
        Console.WriteLine($"删除999号学生：{(delete2 ? "成功" : "失败")}");
        Console.WriteLine("\n6. 最终所有学生信息：");
        foreach (var student in studentManager.GetAllStudents())
        {
            Console.WriteLine(student);
        }

        Console.ReadKey();
    }
}

public static class StudentJsonHelper
{
    private static readonly JsonSerializerOptions _options = new()
    {
        Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
        WriteIndented = true
    };

    public static bool SerializeToFile(List<Student> students, string filePath)
    {
        try
        {
            if (students == null || string.IsNullOrEmpty(filePath)) return false;
            string json = JsonSerializer.Serialize(students, _options);
            File.WriteAllText(filePath, json, System.Text.Encoding.UTF8);
            return true;
        }
        catch (Exception)
        {
            return false; 
        }
    }

    public static List<Student> DeserializeFromFile(string filePath)
    {
        try
        {
            if (!File.Exists(filePath)) return null;
            string json = File.ReadAllText(filePath, System.Text.Encoding.UTF8);
            return JsonSerializer.Deserialize<List<Student>>(json, _options);
        }
        catch (Exception)
        {
            return null; 
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        var studentManager = new StudentManager();
        string jsonFilePath = "Students.json"; 
        Console.WriteLine("===== 学生集合JSON序列化/反序列化测试 =====");
        Console.WriteLine("\n1. 向集合添加测试学生：");
        studentManager.AddStudent(new Student { StudentId = 101, Name = "张三", Age = 18, Class = "高一(1)班" });
        studentManager.AddStudent(new Student { StudentId = 102, Name = "李四", Age = 17, Class = "高一(2)班" });
        studentManager.AddStudent(new Student { StudentId = 103, Name = "王五", Age = 18, Class = "高一(3)班" });
        foreach (var stu in studentManager.GetAllStudents())
        {
            Console.WriteLine(stu);
        }
        Console.WriteLine($"\n2. 序列化学生集合到文件：{jsonFilePath}");
        List<Student> studentsToSerialize = studentManager.GetAllStudents();
        bool serializeSuccess = StudentJsonHelper.SerializeToFile(studentsToSerialize, jsonFilePath);
        Console.WriteLine($"序列化结果：{(serializeSuccess ? "成功" : "失败")}");
        Console.WriteLine("\n3. 从文件反序列化学生集合：");
        List<Student> deserializedStudents = StudentJsonHelper.DeserializeFromFile(jsonFilePath);
        if (deserializedStudents == null)
        {
            Console.WriteLine("反序列化失败（文件不存在或格式错误）");
            return;
        }
        Console.WriteLine("反序列化成功，读取到的学生信息：");
        foreach (var stu in deserializedStudents)
        {
            Console.WriteLine(stu);
        }

        Console.WriteLine("\n测试完成！JSON文件已保存到程序运行目录。");
        Console.ReadKey();
    }
}